using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
namespace JSG.BubbleShoot
{
    public class Ring : MonoBehaviour
    {
        public float m_Radius = 40;

        public List<Candy> m_Candies;
        // Start is called before the first frame update
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {

        }

        public void UpdateRadius(float delta)
        {
            StartCoroutine(Co_UpdateRadius(delta));
            //m_Radius = delta;

            //float m_SpawnCount;
            //float m_SpawnAngle;
            //m_SpawnCount = (2 * Mathf.PI * m_Radius) / 7;

            //m_SpawnAngle = (360 / (float)Math.Floor(m_SpawnCount));

            //for (int i=0;i<m_Candies.Count;i++) 
            //{
            //    Vector3 cordinates = Quaternion.Euler(0, 0, i * m_SpawnAngle) * new Vector3(m_Radius, 0, 0);
            //    m_Candies[i].transform.localPosition = cordinates;

            //    //Vector3 dir = candy.transform.localPosition;
            //    //dir = Vector3.ClampMagnitude(dir, m_Radius);
            //    //candy.transform.localPosition = dir;
            //}
        }

        IEnumerator Co_UpdateRadius(float newRadius)
        {
            Vector3[] startPos = new Vector3[m_Candies.Count];
            Vector3[] endPos = new Vector3[m_Candies.Count];
            for (int i = 0; i < m_Candies.Count; i++)
            {
                startPos[i] = m_Candies[i].transform.localPosition;
            }

            float m_SpawnCount = (2 * Mathf.PI * newRadius) / 7;
            float m_SpawnAngle = (360 / (float)Math.Floor(m_SpawnCount));

            for (int i = 0; i < m_Candies.Count; i++)
            {
                if (i > m_SpawnCount - 1)
                {
                    m_Candies[i].gameObject.SetActive(false);
                    GameControl.m_Current.m_ColorCount[m_Candies[i].m_ColorNum]--;
                }
                Vector3 cordinates = Quaternion.Euler(0, 0, i * m_SpawnAngle) * new Vector3(newRadius, 0, 0);
                endPos[i] = cordinates;
            }

            float lerp = 0;
            while (lerp < 1)
            {
                for (int i = 0; i < m_Candies.Count; i++)
                {
                    m_Candies[i].transform.localPosition = Vector3.Lerp(startPos[i], endPos[i], lerp);
                }
                lerp += Time.deltaTime;
                yield return null;
            }

            for (int i = 0; i < m_Candies.Count; i++)
            {
                m_Candies[i].transform.localPosition = endPos[i];
            }

            m_Radius = newRadius;
        }
    }
}